{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting Pandapower Networks using Plotly\n",
    "This tutorial shows you how to make interactive plots of pandapower networks using plotly (https://plot.ly/python/).\n",
    "The best way to get started is to get familiar with 3 built-in plots that correspond to:\n",
    "* a simple plot of a network (respect switch statuses by default)\n",
    "* voltage-levels plot - colores and labels network according to voltage levels\n",
    "* Power Flow results - a colormap plot where buses are colored according to voltage magnitudes and branches according to line/transformer loading.\n",
    "\n",
    "The following sample plots are with mv_oberrhein network from the pandapower.networks package:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandapower.plotting.plotly import simple_plotly\n",
    "from pandapower.networks import mv_oberrhein\n",
    "from pandapower import runpp\n",
    "net = mv_oberrhein()\n",
    "runpp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple plotting\n",
    "A simple network plot wich labels as separate trace all network buses, lines, transformers and external grid.\n",
    "Try some of the fancy plotly features from the upper-right corner:\n",
    "* zooming,\n",
    "* hoover tool (position cursor on the bus/line/trafo to get basic info),\n",
    "* selecting, \n",
    "* click on the legend to hide-show any of the legened elements,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simple_plotly(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Voltage levels\n",
    "Plots a network colored and layered according to voltage levels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandapower.plotting.plotly import vlevel_plotly\n",
    "vlevel_plotly(net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandapower.networks import create_cigre_network_hv\n",
    "net = create_cigre_network_hv()\n",
    "runpp(net)\n",
    "vlevel_plotly(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Power Flow Results\n",
    "Results from `res_bus`, `res_line` and `res_trafo` can be effectively displayed using `pf_res_plolty`.\n",
    "Buses colored according to resulting voltage magnitude using colormap in range $[0.9,1.1]$.  \n",
    "Lines and trafos are colored according to resulting loading using colormap in range $[0,100]$.  \n",
    "Positioning a cursor over a bus or line-breaks shows more details about each element."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandapower.plotting.plotly import pf_res_plotly\n",
    "pf_res_plotly(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## General Plotting features\n",
    "Interactive plots are built to share some general plotting features with static plots using [matplotlib](https://github.com/e2nIEE/pandapower/blob/master/tutorials/plotting_basic.ipynb).  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plots without geodata available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = mv_oberrhein()\n",
    "runpp(net)\n",
    "\n",
    "# delete the geocoordinates\n",
    "net.bus_geodata.drop(net.bus_geodata.index, inplace=True)\n",
    "net.line_geodata.drop(net.line_geodata.index, inplace=True)\n",
    "\n",
    "simple_plotly(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Figure size and aspect ratio\n",
    "**Aspect ratio** (`aspectratio`) - default aspect ratio of a figure is set to `'auto'` which means keeping aspect ratio proportional to geodata. If `aspectratio=False` figure will be stretch according to window size."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.bus_geodata.drop(net.bus_geodata.index, inplace=True)\n",
    "net.line_geodata.drop(net.line_geodata.index, inplace=True)\n",
    "pf_res_plotly(net, aspectratio=(1.3,1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Figure Size** (`figsize`) is by default set to 1 and it is used only to multiply total plot size, thus real figure size `figsize=aspectratio*figsize`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = mv_oberrhein()\n",
    "simple_plotly(net, aspectratio=(2,1), figsize=0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "More tutorials about interactive plots using ploltly:\n",
    "\n",
    "* [custom interactive plots](http://nbviewer.jupyter.org/github/e2nIEE/pandapower/blob/develop/tutorials/plotly_traces.ipynb)\n",
    "* [interactive plots on maps](http://nbviewer.jupyter.org/github/e2nIEE/pandapower/blob/develop/tutorials/plotly_maps.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
